﻿using MeetingRoomBooking.Application.Common.Interfaces;

namespace MeetingRoomBooking.Application.Bookings.Queries;
public class GetBookingByUserIdQuery : IRequest<List<BookingViewModel>>
{
    public int UserId { get; set; }
}

public class GetBookingByUserIdQueryHandler : IRequestHandler<GetBookingByUserIdQuery, List<BookingViewModel>>
{
    private readonly IApplicationDbContext _context;

    public GetBookingByUserIdQueryHandler(IApplicationDbContext context)
    {
        _context = context;
    }

    public async Task<List<BookingViewModel>> Handle(GetBookingByUserIdQuery request, CancellationToken cancellationToken)
    {
        var bookings = await _context.Bookings.Include(x => x.Room).Include(y => y.User).Where(u => u.UserId == request.UserId).ToListAsync(cancellationToken);
        var bookingViewModels = bookings.Select(x => new BookingViewModel
        {
            End = x.EndDate.ToString("yyyy-MM-dd hh:mm"),
            Start = x.StartDate.ToString("yyyy-MM-dd hh:mm"),
            Title = x.Title,
            UserName = x?.User?.UserName ?? "",
            Id = x?.Id.ToString() ?? "",
            Description = x?.Description,
            People = [x?.User?.UserName ?? ""],
            Location = x?.Room?.Name ?? "",
        }).ToList();

        return bookingViewModels;
    }
}
